package scanner.com.activity;

import android.app.TabActivity;
import android.content.Intent;
import android.os.Bundle;
import android.widget.TabHost;

import com.haha.extend.textview.CustomFontTextView;
import com.haha.extend.textview.FooterNavBar;

import scanner.com.scanner.R;

public class MainActivity extends TabActivity {
    private TabHost tabHost;
    private FooterNavBar footerNavBar;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_tabs);
        findView();
        initView();
    }

    private void findView(){
        tabHost = (TabHost)findViewById(android.R.id.tabhost);
        footerNavBar = (FooterNavBar)findViewById(R.id.footer_nav_bar);
    }

    private void initView(){
        footerNavBar.setOnNavItemChangedListener(onNavItemChangedListener);
        tabHost.setup(getLocalActivityManager());
        tabHost.addTab(tabHost.newTabSpec("SCAN").setContent(new Intent(this,ScanActivity.class)).setIndicator("SCAN"));
        tabHost.addTab(tabHost.newTabSpec("HISTORY").setContent(new Intent(this,HistoryActivity.class)).setIndicator("HISTORY"));
        tabHost.addTab(tabHost.newTabSpec("MAKE").setContent(new Intent(this,MakeActivity.class)).setIndicator("MAKE"));
        tabHost.addTab(tabHost.newTabSpec("SETTINGS").setContent(new Intent(this,SettingsActivity.class)).setIndicator("SETTINGS"));
        tabHost.setCurrentTabByTag("SCAN");
    }

    private FooterNavBar.OnNavItemChangedListener onNavItemChangedListener  = new FooterNavBar.OnNavItemChangedListener() {
        private static final int FIRST_TAB = 0;
        private static final int SECOND_TAB = 1;
        private static final int THIRD_TAB = 2;
        private static final int FOURTH_TAB = 3;

        private static final float UNCHECKED_ALPHA = 0.5f;
        private static final float CHECKED_ALPHA = 1.0f;

        @Override
        public void onNavItemChanged(CustomFontTextView[] items, CustomFontTextView item, int position) {
            for(int i = 0; i < 4; i++) items[i].setAlpha(UNCHECKED_ALPHA);
            switch (position){
                case FIRST_TAB:
                    tabHost.setCurrentTabByTag("SCAN");
                    item.setAlpha(CHECKED_ALPHA);
                    break;
                case SECOND_TAB:
                    tabHost.setCurrentTabByTag("HISTORY");
                    item.setAlpha(CHECKED_ALPHA);
                    break;

                case THIRD_TAB:
                    tabHost.setCurrentTabByTag("MAKE");
                    item.setAlpha(CHECKED_ALPHA);
                    break;

                case FOURTH_TAB:
                    tabHost.setCurrentTabByTag("SETTINGS");
                    item.setAlpha(CHECKED_ALPHA);
                    break;
            }
        }
    };
}
